/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.lucava.boatskeeper.client.view;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import org.lucava.boatskeeper.client.event.ConnectEvent;
import org.lucava.boatskeeper.shared.dto.UserDto;
import org.lucava.lib.client.view.MvcView;

/**
 *
 * @author glenn
 */
public class AuthView extends MvcView {

    private final Button connectButton;
    private final TextBox userid;
    private final PasswordTextBox passwd;
    private final Label message;

    public AuthView(EventBus eventBus) {
        super(eventBus);
        VerticalPanel holder = new VerticalPanel();
        holder.setStyleName("login-container");
        holder.add(new Label("User ID"));
        userid = new TextBox();
        userid.setName("userid");
        holder.add(userid);

        holder.add(new Label("Password"));
        passwd = new PasswordTextBox();
        passwd.setName("passwd");
        holder.add(passwd);

        connectButton = new Button("Connexion", new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                resetMessages();
                getEventBus().fireEvent(new ConnectEvent());
            }
        });
        holder.add(connectButton);
        holder.setSpacing(5);
        //error message
        message = new Label();
        holder.add(message);

        initWidget(holder);
    }

    private void resetMessages() {
        message.setStyleName("red-text");
        message.setText("");
        message.setVisible(false);

    }

    public Boolean isValid() {

        Boolean isValid = true;

        if (userid.getText().isEmpty() || passwd.getText().isEmpty()) {
            message.setVisible(true);
            message.setText("identifiant et mot de passe requis");
            isValid = false;
        }

        return isValid;

    }

    public UserDto getUser() {
        return new UserDto(null, userid.getText(), passwd.getText());
    }
}
